package com.ybzl.mini.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ybzl.common.annotation.Log;
import com.ybzl.common.core.controller.BaseController;
import com.ybzl.common.core.controller.IdGenerator;
import com.ybzl.common.core.domain.Result;
import com.ybzl.common.core.page.PageData;
import com.ybzl.common.enums.BusinessType;
import com.ybzl.common.utils.SecurityUtils;
import com.ybzl.mini.entity.MiniPyq;
import com.ybzl.mini.entity.MiniPyqImg;
import com.ybzl.mini.entity.dto.MiniPyqDto;
import com.ybzl.mini.service.IMiniPyqImgService;
import com.ybzl.mini.service.IMiniPyqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/*
 * Desc：污友圈主表
 * @Author xxx
 * @Company yb.com
 * @Since  2024-09-19
 *
 */
@Api(tags = "污友圈主表")
@RestController
@RequestMapping("/miniPyq")
public class MiniPyqController extends BaseController {

    @Autowired
    private IMiniPyqService iMiniPyqService;

    @Autowired
    private IMiniPyqImgService miniPyqImgService;

    /**
     * 查询污友圈主表
     *
     * @return
     */
    @ApiOperation("查询污友圈主表列表")
    @Log(title = "污友圈主表查询", businessType = BusinessType.OTHER)
    @PostMapping("/v1/list")
    public PageData list(@Validated @RequestBody MiniPyq pyq) {
        LambdaQueryWrapper<MiniPyq> query = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(pyq.getId())){
            query.eq(MiniPyq::getId,pyq.getId());
        }
        startPage();
        return getDataTable(iMiniPyqService.list(query));
    }

    /**
     * 新增污友圈主表
     *
     * @param miniPyqDto miniPyqDto
     * @return
     */
    @Log(title = "新增污友圈主表", businessType = BusinessType.INSERT)
    @ApiOperation("新增污友圈主表")
    @PostMapping("/v1/add")
    public Result add(@Validated @RequestBody MiniPyqDto miniPyqDto) {
        MiniPyq miniPyq = new MiniPyq();
        Long id = IdGenerator.orderId(MiniPyq.class);
        miniPyq.setId(id);
        miniPyq.setUserId(SecurityUtils.getUserId());
        miniPyq.setUserName(SecurityUtils.getUsername());
        miniPyq.setZan("502");
        miniPyq.setTime(new Date());
        miniPyq.setTxt(miniPyqDto.getTxt());


        iMiniPyqService.save(miniPyq);

        List<MiniPyqImg> list = new ArrayList<>();

        miniPyqDto.getPics().forEach(item -> {
            MiniPyqImg m = new MiniPyqImg();
            m.setId(IdGenerator.orderId(MiniPyqImg.class));
            m.setFabuId(id);
            m.setImgUrl(item);

            list.add(m);
        });

        miniPyqImgService.saveBatch(list);
        return Result.success();
    }

    /**
     * 修改污友圈主表
     *
     * @param miniPyq
     * @return
     */

    @Log(title = "更新污友圈主表", businessType = BusinessType.UPDATE)
    @ApiOperation("更新污友圈主表")
    @PutMapping("/v1/update")
    public Result edit(@Validated @RequestBody MiniPyq miniPyq) {
        iMiniPyqService.updateById(miniPyq);
        return Result.success();
    }

    /**
     * 删除污友圈主表
     *
     * @param ids
     * @return
     */
    @Log(title = "删除污友圈主表", businessType = BusinessType.DELETE)
    @ApiOperation("删除污友圈主表")
    @DeleteMapping("/v1/delete/{ids}")
    public Result remove(@PathVariable String ids) {
        iMiniPyqService.removeBatchByIds(Arrays.asList(ids.split(",")));
        miniPyqImgService.deleteByFabuId(ids);
        return success();
    }
}
